package com.shire.shire_interface.controller.keyword;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.friend.WxFriendExample;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.keyword.WxKeywordReply;
import com.shire.shire_interface.model.keyword.WxKeywordReplyExample;
import com.shire.shire_interface.model.keyword.WxReplyRecord;
import com.shire.shire_interface.model.keyword.WxReplyRecordExample;
import com.shire.shire_interface.service.friend.WxFriendService;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.keyword.WxKeywordReplyService;
import com.shire.shire_interface.service.keyword.WxReplyRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//@Api(tags = "群管理 关键词回复")
@RestController
@RequestMapping("/keyword")
public class WxReplyRecordController extends BaseController {

    @Resource
    private WxKeywordReplyService wxKeywordReplyService;
    @Resource
    private WxReplyRecordService wxReplyRecordService;
    @Resource
    private WxGroupService wxGroupService;
    @Resource
    private WxFriendService friendService;

//    @ApiOperation(value = "回复记录列表 csId, wxAccount")
    @ResponseBody
    @PostMapping("/list")
    public Response getList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            Integer type = 0;

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            if (jsonObject.containsKey("type")) {
                type = jsonObject.getInteger("type");
            }

            WxKeywordReplyExample wxKeywordReplyExample = new WxKeywordReplyExample();
            wxKeywordReplyExample.createCriteria().andWxaccountEqualTo(wxAccount).andTypeEqualTo(type).andStatusEqualTo(0);
            wxKeywordReplyExample.setOrderByClause("create_time desc");
            addPageParamToExample(jsonObject,wxKeywordReplyExample);

            List<WxKeywordReply> wxKeywordReplies = wxKeywordReplyService.selectByExample(wxKeywordReplyExample);

            for (WxKeywordReply wxKeywordReply : wxKeywordReplies) {
                List<WxReplyRecord> wxReplyRecordList = new ArrayList<>();
                Integer id = wxKeywordReply.getId();
                WxReplyRecordExample wxReplyRecordExample = new WxReplyRecordExample();
                wxReplyRecordExample.createCriteria().andWxaccountidEqualTo(wxAccount).andReplyruleidEqualTo(id).andStatusEqualTo(0);
                List<WxReplyRecord> wxReplyRecords = wxReplyRecordService.selectByExample(wxReplyRecordExample);
                if (type == 0) {
                    for (WxReplyRecord wxReplyRecord : wxReplyRecords) {
                        WxGroupExample wxGroupExample = new WxGroupExample();
                        wxGroupExample.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(wxReplyRecord.getGroupaccount());
                        List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                        wxReplyRecord.setGroups(wxGroups);
                        wxReplyRecordList.add(wxReplyRecord);
                    }
                }else {
                    for (WxReplyRecord wxReplyRecord : wxReplyRecords) {
                        WxFriendExample friendExample = new WxFriendExample();
                        friendExample.createCriteria().andWxaccountEqualTo(wxAccount).andFriendwxidEqualTo(wxReplyRecord.getGroupaccount());
                        List<WxFriend> wxFriends = friendService.selectByExample(friendExample);
                        wxReplyRecord.setFriends(wxFriends);
                        wxReplyRecordList.add(wxReplyRecord);
                    }
                }
                wxKeywordReply.setWxReplyRecord(wxReplyRecordList);
            }
            response.setData(wxKeywordReplies);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除记录 id")
    @ResponseBody
    @PostMapping("/delRecord")
    public Response delSetting(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer id = jsonObject.getInteger("id");
            wxKeywordReplyService.deleteByPrimaryKey(id);
            response.success("操作成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
